//
//  LinkingCardAlert.swift
//  iPhone
//
//  Created by Toj on 11/8/22.
//

import UIKit

class LinkingCardAlert: UIView {
    
    @objc private func didTapUnlink() {
        var status: LinkingCardResultStatus = .none
        let random = arc4random_uniform(4)
        // print("-->toj: random: \(random)")
        switch random {
        case 1:
            status = .resultSuccess
        case 2:
            status = .resultFaild
        case 3:
            status = .tips
        default:
            status = .login
        }
        
        let alert = LinkingCardResultAlert()
        alert.status = .login
        STAlertHelper.manager.show(alert: alert)
        print("-->toj: status: \(status.rawValue)")
        
        removeFromSuperview()
    }
    
    @objc private func didTapLink() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        return view
    }()
    
    private let cornerRadius: CGFloat = 8
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_cornerRadius = cornerRadius
        return view
    }()
    
    private lazy var shadowView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_shadow(color: .black, opacity: 0.3, radius: 24, corner: cornerRadius)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.text = "Congratulations! You can bind a smart card to obtain vip benefits"
        view.numberOfLines = 0
        view.textAlignment = .center
        view.textColor = "#212121".to_color
        view.font = UIFont.boldSystemFont(ofSize: 20.f_scale)
        return view
    }()
    
    private lazy var carInfoView = LinkingCardInforView()
    
    private lazy var notLinkButton: UIButton = {
        let view = UIButton()
        //"Do not link smartcard now".ln_localized.to_attributedString(font: UIFont.systemFont(ofSize: 12.f_scale), color: "#AAAAAA".to_color, lineStyle: .underline)
//        view.setAttributedTitle(attributedString, for: .normal)
        view.addTarget(self, action: #selector(didTapUnlink), for: .touchUpInside)
        return view
    }()
    
    private let linkButtonHeight = 32.ui_scale
    private lazy var linkButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#E8CD8C".to_color
        view.setTitle("LINK", for: .normal)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.addTarget(self, action: #selector(didTapLink), for: .touchUpInside)
        view.cm_cornerRadius = linkButtonHeight * 0.5
        return view
    }()
    
    deinit {
        // print("-->toj: LinkingCardAlert " + #function)
    }
}

extension LinkingCardAlert {
    
    private func setupAddSubviews() {
        
        addSubview(backgroundView)
        addSubview(shadowView)
        addSubview(contentView)
        addSubview(titleLabel)
        addSubview(carInfoView)
        addSubview(notLinkButton)
        addSubview(linkButton)
    }
    
    private func setupLayoutSubviews() {
        backgroundView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        carInfoView.snp.makeConstraints { make in
            make.center.equalTo(self)
            make.width.equalTo(255.ui_mscale)
            make.height.equalTo((116).ui_scale)
        }
        
        notLinkButton.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.top.equalTo(carInfoView.snp.bottom).offset(20.ui_scale)
            make.width.equalTo(255.ui_mscale)
            make.height.equalTo(20.ui_mscale)
        }
        
        linkButton.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.top.equalTo(notLinkButton.snp.bottom).offset(20.ui_scale)
            make.width.equalTo(250.ui_mscale)
            make.height.equalTo(linkButtonHeight)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.bottom.equalTo(carInfoView.snp.top).offset(-20.ui_scale)
            make.width.equalTo(255.ui_mscale)
        }
        
        contentView.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.width.equalTo(280.ui_mscale)
            make.top.equalTo(titleLabel.snp.top).offset(-20.ui_scale)
            make.bottom.equalTo(linkButton.snp.bottom).offset(20.ui_scale)
        }
        
        shadowView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
}
